package  com.ai_daily.aitiantian.base

import android.content.res.Configuration
import android.content.res.Resources
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity


/**
 * 基类Activity
 */
abstract class BaseActivity : AppCompatActivity() {
    lateinit var mActivity: BaseActivity
//    private var mLoadingDialog: LoadingDialog? = null

//    override fun attachBaseContext(newBase: Context) {
//        val language = AppCacheManager().getCurrentLanguage()
//        val newContext = LanguageUtils.switchAppLanguage(newBase, language)
//        super.attachBaseContext(newContext)
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 禁止所有页面截屏
        window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
//         window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) //显示状态栏
//        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        mActivity = this
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if (newConfig.fontScale != 1f) {
            resources
        }
        super.onConfigurationChanged(newConfig)
    }

    override fun getResources(): Resources {
        val res = super.getResources()
        if (res.configuration.fontScale != 1f) {
            val newConfig = Configuration()
            newConfig.setToDefaults()
            res.updateConfiguration(newConfig, res.displayMetrics)
        }
        return res
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//        if (requestCode == UpgradeUtil.DOWNLOAD_PERMISSION_CODE) {
//            if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                val intent = Intent(this, UpgradeService::class.java)
//                intent.putExtra("url", downloadUrl)
//                intent.putExtra("versionCode", UpgradeUtil.versionCode)
//                startService(intent)
//            } else {
//                if (UpgradeUtil.forceUpdate) {//强制更新，退出应用
//                    finishAll()
//                } else {
//                    AppUtils.showToast(this, R.string.permission_fail_tip)
//                }
//            }
//        }
    }

    // 点击返回按钮
    open fun clickBackBtn() {
        // 统一上报
        // StatisticUtils.sendClickData(mActivity, StatisticConstants.CUSTOM_LEFT_BUTTON_BACK_CLICK)
        onBackPressed()
    }

    override fun onDestroy() {
        super.onDestroy()
    }


//    open fun showLoadingDialog() {
//        showLoadingDialog(getString(R.string.load_data_ing))
//    }

//    open fun showLoadingDialog(arg: String) {
//        if (isFinishing || isDestroyed) {
//            return
//        }
//        if (mLoadingDialog == null) {
//            mLoadingDialog = LoadingDialog(mActivity, arg)
//        }
//        mLoadingDialog?.dialog?.show()
//    }

//    open fun loadingDialogDismiss() {
//        if (isFinishing || isDestroyed) {
//            return
//        }
//        if (mLoadingDialog != null && mLoadingDialog?.dialog?.isShowing!!) {
//            mLoadingDialog?.closeDialog()
//        }
//    }
}